﻿// -----------------------------------------------------------------------
// <copyright file="CastleUtility.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.ComponentModel;
using Castle.Windsor;

namespace BasicFrameworkDemo.Utility
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;

    using Castle.Facilities.WcfIntegration;
    using Castle.Windsor;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CastleUtility
    {

        private static  CastleUtility CastleInstance;

        private CastleUtility()
        {

        }


        public static CastleUtility GetInstance()
        {
            return CastleInstance ?? (CastleInstance = new CastleUtility());
        }

        /// <summary>
        /// The value indicating whether the log4net has been registered.
        /// </summary>
        /// Creator:zhouchao
        /// Creation Date:11/15/2011 9:41 AM
        /// Modifier:
        /// Last Modified:
        /// ----------------------------------------------------------------------------------------
        private bool registered;

        /// <summary>
        /// The container.
        /// </summary>
        /// Creator:zhouchao
        /// Creation Date:10/14/2011 8:46 AM
        /// Modifier:
        /// Last Modified:
        /// ----------------------------------------------------------------------------------------
        private WindsorContainer container;

        /// <summary>
        /// Gets the container contains has registered objects after the config of castle has been loaded.
        /// </summary>
        /// Creator:zhouchao
        /// Creation Date:11/14/2011 10:12 AM
        /// Modifier:
        /// Last Modified:
        /// ----------------------------------------------------------------------------------------
        public IWindsorContainer Container
        {
            get
            {
                return container ?? (container = new WindsorContainer(SystemConfig.IOCComponentCfgPath));
            }
        }

        /// <summary>
        /// The method must run at first in the appcation.
        /// this is load the config about castle.
        /// </summary>
        /// Creator:zhouchao
        /// Creation Date:10/14/2011 8:47 AM
        /// Modifier:
        /// Last Modified:
        /// ----------------------------------------------------------------------------------------
        public void Config()
        {
            if (registered)
            {
                return;
            }

            //DefaultServiceHostFactory.RegisterContainer(Container.Kernel);
            Container.Install();

            registered = true;
        }
    }
}
